package com.boolib.service.impl;

import com.alibaba.fastjson.JSON;
import com.boolib.bo.Product2Bo;
import com.boolib.bo.ProductBO;
import com.boolib.bo.ShopSkuKeyBo;
import com.boolib.commons.Const;
import com.boolib.commons.entity.ServerResponse;
import com.boolib.commons.util.ServerResponseUtil;
import com.boolib.dao.*;
import com.boolib.pojo.ShopProduct;
import com.boolib.pojo.ShopProductExample;
import com.boolib.pojo.ShopSku;
import com.boolib.pojo.ShopSkuVal;
import com.boolib.service.IBrandService;
import com.boolib.service.ICategoryService;
import com.boolib.service.IProductService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;


@Slf4j
@Service
public class ProductServiceImpl implements IProductService {



    @Autowired
    private  CategoryServiceImpl  categoryService;

    @Autowired
    private ICategoryService iCategoryService;


    @Autowired
    private IBrandService iBrandService;


    @Autowired
    private ProductServiceImpl productService;


    @Autowired
    private  ShopProductMapper shopProductMapper;


    @Autowired
    private ShopProductQueryMapper shopProductQueryMapper;



    @Autowired
    private ShopSkuKeyMapper shopSkuKeyMapper;


    @Autowired
    private ShopSkuValMapper shopSkuValMapper;



    @Autowired
    private ShopSkuMapper shopSkuMapper;






    @Override
    public ServerResponse addProduct(ProductBO productBO, String createBy) {

        Date date = new Date();


        ShopProduct shopProduct = productBO.getShopProduct();



        ServerResponse    serverResponse  =      ServerResponseUtil.argumentCheck(shopProduct.getProductTitle(),shopProduct.getProductImages(),shopProduct.getProductMainImg());

        if(!serverResponse.isSuccess()) return serverResponse;


        shopProduct.setId(null);

        shopProduct.setStatus(Const.Product.NORMAL);


        if(shopProduct.getCampaignBtn()!=null&&shopProduct.getCampaignBtn() >1){
            return ServerResponseUtil.argumentNUll();
        }

        shopProduct.setCreateTime(date);

        serverResponse = categoryService.selectOneNormal(shopProduct.getCategoryId());

        if(!serverResponse.isSuccess()) return  ServerResponseUtil.argumentCheck();

        shopProduct.setCreateBy(createBy);

        shopProduct.setUpdateBy(createBy);

        shopProduct.setUpdateTime(date);



        serverResponse = productService.checkTitle(shopProduct.getProductTitle());

        if(!serverResponse.isSuccess()) throw new RuntimeException("商品添加失败");


        if(shopProductMapper.insertSelective(shopProduct)<=0){
            log.info(shopProduct.getProductTitle()+"商品添加失败");
            throw new RuntimeException("商品添加失败");
        }


        List<ShopSkuKeyBo> lists =  productBO.getShopSkuKeys();


        if(lists.size()<1){
            return  ServerResponseUtil.argumentNUll();
        }

        Map map = Maps.newLinkedHashMap();


        for (ShopSkuKeyBo shopSkuKey  : lists){

            shopSkuKey.setSpuId(shopProduct.getId());

            shopSkuKey.setCreateBy(createBy);
            shopSkuKey.setUpdateBy(createBy);

            shopSkuKey.setUpdateTime(date);
            shopSkuKey.setCreateTime(date);
            shopSkuKey.setId(null);

            if(shopSkuKeyMapper.insertSelective(shopSkuKey)<=0){
                log.info("商品sku添加失败");
                throw new RuntimeException("商品sku添加失败");
            }



            List list = Lists.newArrayList();


            List<ShopSkuVal> s = shopSkuKey.getShopSkuVals();
            if(s.size()<1) {

                log.info(new StringBuffer().append(shopProduct.getProductTitle()).append(shopSkuKey.getKeyName()).append("skuVal不能为空") .toString());
                throw new RuntimeException("商品sku参数为空");

            }



            for(ShopSkuVal shopSkuVal :s){


                shopSkuVal.setSpuId(shopProduct.getId());
                shopSkuVal.setKeyId(shopSkuKey.getId());
                shopSkuVal.setCreateTime(date);

                shopSkuKey.setCreateBy(createBy);

                shopSkuVal.setId(null);
                shopSkuVal.setUpdateTime(date);

                if(shopSkuValMapper.insertSelective(shopSkuVal)<=0){
                    throw  new RuntimeException("skuvalue添加失败");
                }

                list.add(shopSkuVal.getId());

            }
            map.put(shopSkuKey.getId(),list);




        }








        return ServerResponse.createBySuccess(productBO);
    }




















    @Override
    public ServerResponse selectProductNormalByBrandId(Integer brandId) {

        ServerResponse serverResponse = ServerResponseUtil.argumentCheck(brandId);

        if(!serverResponse.isSuccess()) return  serverResponse;

        serverResponse = iBrandService.selectBrandById(brandId);

        if(!serverResponse.isSuccess()) return  serverResponse;



        ShopProduct shopProduct = new ShopProduct();

        shopProduct.setBrandId(brandId);

        shopProduct.setStatus(Const.Product.NORMAL);

        List<ShopProduct > shopProducts = shopProductMapper .select(shopProduct);

        return ServerResponse.createBySuccess(shopProducts);
    }

    @Override
    public ServerResponse selectProductNormalByCategoryId(Integer categoryId, Integer pageNum, Integer pageSize, boolean sort) {

        ServerResponse serverResponse = ServerResponseUtil.argumentCheck(categoryId,pageNum,pageSize);

        if(!serverResponse.isSuccess()) return serverResponse;

        serverResponse = iCategoryService.selectOneNormal(categoryId);

        if(!serverResponse.isSuccess()) return serverResponse;




        ShopProductExample shopProductExample = new ShopProductExample();

        ShopProductExample.Criteria criteria = shopProductExample.createCriteria();

        criteria.andStatusGreaterThan(0).andCategoryIdEqualTo(categoryId);


        if(sort){
            PageHelper.startPage(pageNum,pageSize).setOrderBy("createTime");
        }
        else{
            PageHelper.startPage(pageNum,pageSize).setOrderBy("productSpuCode");
        }

        PageInfo pageInfo = new PageInfo( shopProductMapper.selectByExample(shopProductExample));



        return ServerResponse.createBySuccess(pageInfo);
    }

    @Override
    public ServerResponse selectProductNormalById(Integer productId) {

        ServerResponse serverResponse = ServerResponseUtil.argumentCheck(productId);

        if(!serverResponse.isSuccess()) return  serverResponse;


        ShopProduct shopProduct = new ShopProduct();

        shopProduct.setId(productId);

        shopProduct.setStatus(Const.Product.NORMAL);

        ShopProduct result = shopProductMapper.selectOne(shopProduct);




        /**
         * 调用需要自行判断null
         */

        return ServerResponse.createBySuccess(result);
    }

    @Override
    public ServerResponse queryProductInfoById(Integer productId) {
        ServerResponse serverResponse = ServerResponseUtil.argumentCheck(productId) ;

        if(!serverResponse.isSuccess()) return serverResponse;

        Product2Bo product2Bo = shopProductQueryMapper.queryProductInfoById(productId);

        ShopSku shopSku = new ShopSku();
        shopSku.setSpuId(productId);
        shopSku.setStatus(Const.Product.NORMAL);
        product2Bo.setShopSkus(shopSkuMapper.select(shopSku));
        log.info(JSON.toJSONString(product2Bo));
        return ServerResponse.createBySuccess(product2Bo);
    }


    @Override
    public ServerResponse selectAllNormal(int pageNum, int pageSize, boolean sort) {

        ShopProduct shopProduct = new ShopProduct();

        shopProduct.setStatus(Const.Product.NORMAL);


        if(sort){
            PageHelper.startPage(pageNum,pageSize).setOrderBy("createTime");
        }
        else{
            PageHelper.startPage(pageNum,pageSize).setOrderBy("productSpuCode");
        }

        PageInfo pageInfo = new PageInfo( shopProductMapper.select(shopProduct));



        return ServerResponse.createBySuccess(pageInfo);
    }



    @Override
    public ServerResponse selectAllAbnormal(int pageNum, int pageSize, boolean sort) {



        ShopProductExample shopProductExample = new ShopProductExample();

        ShopProductExample.Criteria criteria = shopProductExample.createCriteria();

        criteria.andStatusGreaterThan(0);


        if(sort){
            PageHelper.startPage(pageNum,pageSize).setOrderBy("createTime");
        }
        else{
            PageHelper.startPage(pageNum,pageSize).setOrderBy("productSpuCode");
        }

        PageInfo pageInfo = new PageInfo( shopProductMapper.selectByExample(shopProductExample));



        return ServerResponse.createBySuccess(pageInfo);
    }





    public ServerResponse checkId(Integer id){

        ServerResponse serverResponse  = ServerResponseUtil.argumentCheck(id);

        if(!serverResponse.isSuccess())  return  serverResponse;

        ShopProduct shopProduct = new ShopProduct();

        shopProduct.setId(id);

        ShopProduct result = shopProductMapper.selectOne(shopProduct);

        serverResponse  = ServerResponseUtil.resultCheck(result,ShopProduct.class);



        return serverResponse;
    }


    public ServerResponse checkTitle(String productTitle) {

        ShopProduct shopProduct = new ShopProduct();
        shopProduct.setProductTitle(productTitle);
        List list = shopProductMapper.select(shopProduct);

        if(list.size()>0) return  ServerResponseUtil.ISEXIST("已存在");
        return  ServerResponse.createBySuccess();
    }




}
